﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：BrandsManage.cs
  //文件功能描述：品牌管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class BrandsManage
    {
        private readonly DALBrands dalBrand = new DALBrands();
        private readonly DALGoods dalGoods = new DALGoods();
        private readonly DALCategories dalCategory = new DALCategories();

        #region "保存品牌信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 保存品牌信息；
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public ReturnValue Save(BrandInfo b)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "保存失败！");
            //判断名称是否重复；
            if (dalBrand.QueryList(1, "BrandName='" + b.Brand.BrandName + "' and Status<>'" + Status.已删除.ToString() + "' and BrandID <>" + b.Brand.BrandID, "") != null)
            {
                ret.Message = "品牌名称重复！";
                return ret;
            }
            FilesManage.FormatBrandLogo(b);
            b.Brand.CreateDate = DateTime.Now;
            b.Brand.LastModifyDate = DateTime.Now;
            if (b.Brand.BrandID > 0)
            {
                Brands o = dalBrand.GetModel(b.Brand.BrandID);
                b.Brand.CreateDate = o.CreateDate;
            }
            return dalBrand.Save(b);
        }
        #endregion

        #region "查询指定的品牌信息； by 赵国伟 2012-04-02"
        /// <summary>
        /// 查询指定的品牌信息；
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public BrandInfo GetBrand(int brandId)
        {
            BrandInfo b = null;
            Brands source = dalBrand.GetModel(brandId);
            if (source != null)
            {
                b = new BrandInfo();
                b.Brand = source;
                b.Categories = dalCategory.QueryCategories(brandId);
                b.GoodsCounts = dalGoods.GetCountsByBrandID(source.BrandID);
            }
            return b;
        }
        #endregion

        #region "删除指定的品牌 by 赵国伟 2012-04-02"
        /// <summary>
        /// 删除指定的品牌
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public ReturnValue Delete(int brandId)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "删除失败！");
            try
            {
                if (dalBrand.Delete(brandId) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "删除成功！";
                }
            }
            catch
            {
                ModifyStatus(brandId, Status.已删除);
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }

            return ret;
        }
        #endregion

        #region "设置指定品牌的状态；by 赵国伟  2012-04-02"
        /// <summary>
        /// 设置指定品牌的状态；
        /// </summary>
        /// <param name="brandId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public ReturnValue ModifyStatus(int brandId, Status status)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "设置失败！");
            if (dalBrand.ModifyStatus(brandId, status.ToString()) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "设置成功！";
            }
            return ret;
        }
        #endregion

        #region "返回指定的排序语句； by 赵国伟 2012-04-02"
        /// <summary>
        /// 返回指定的排序语句；
        /// </summary>
        /// <param name="sorts"></param>
        /// <returns></returns>
        private string GetOrderBy(int sorts)
        {
            string orderBy = "OrderValue , BrandID desc ";
            switch (sorts)
            {
                case 0 :
                    orderBy = "BrandID desc ";
                    break;
                case 1:
                    orderBy = "BrandID ";
                    break;
                case 2:
                    orderBy = "BrandName desc,BrandID desc";
                    break;
                case 3:
                    orderBy = "BrandName,BrandID desc";
                    break;

            }
            return orderBy;
        }
        #endregion

        #region "查询指定状态下品牌信息； by 赵国伟  2012-04-02"
        /// <summary>
        /// 查询指定状态下品牌信息；
        /// </summary>
        /// <param name="IsAvailable"></param>
        /// <param name="sorts"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryBrands(bool IsAvailable,int sorts)
        {
            string cond = "";
            if (IsAvailable)
                cond += " Status='" + Status.正用.ToString() + "'";
            string orderBy = GetOrderBy(sorts);

            return dalBrand.Find(cond, orderBy);
        }
        #endregion

        #region "分页查询品牌信息； by 赵国伟 2012-04-03"
        /// <summary>
        /// 分页查询品牌信息；
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="status"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<BrandInfo> QueryListAsPage(string keyWords, string status,int sorts, AsPage p)
        {
            string cond = "1=1 and Status<>'" + Status.已删除.ToString() + "' ";
            if (!string.IsNullOrEmpty(keyWords))
                cond += " and BrandName like '%" + keyWords + "%' ";
            if(!string.IsNullOrEmpty(status))
                cond += " and Status='"+status+"' ";
            string orderby = GetOrderBy(sorts);
            List<Brands> listOfSource = dalBrand.QueryListAsPage(cond, orderby, p.CurPage, p.PageSize, out p.TotalCount);
            List<BrandInfo> listOfTarget = null;
            if (listOfSource != null)
            {
                listOfTarget = new List<BrandInfo>();
                foreach (Brands source in listOfSource)
                {
                    BrandInfo target = new BrandInfo();
                    target.Brand = source;
                    target.Categories = dalCategory.QueryCategories(source.BrandID);
                    target.GoodsCounts = dalGoods.GetCountsByBrandID(source.BrandID);
                    listOfTarget.Add(target);
                }
            }
            return listOfTarget;
        }
        #endregion

        #region "查询指定分类下品牌信息； by 赵国伟 2012-04-08"
        /// <summary>
        /// 查询指定分类下品牌信息；
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="IsAvailable"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryBrands(int categoryId,bool IsAvailable)
        {
            string cond = " BrandID in(Select BrandID From R_Categories_Brands Where CategoryID=" + categoryId + ") ";
            if (IsAvailable)
                cond += " and Status='" + Status.正用.ToString() + "' ";
            else
                cond += " and status<>'" + Status.已删除.ToString() + "'";
            return dalBrand.Find(cond, "OrderValue,BrandID desc"); 
        }
        #endregion

        #region "查询指定类别下的品牌信息；并输出为下拉选项 by 赵国伟 2012-04-08"
        /// <summary>
        /// 查询指定类别下的品牌信息；并输出为下拉选项
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="IsAvailable"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetBrandsByCategoryIdToSelectOption(int categoryId,bool IsAvailable, string defaultValue)
        {
            BrandsManage bllBrand = new BrandsManage();
            return BLLHelper.GenerateToSelectOption(bllBrand.QueryBrands(categoryId, IsAvailable), defaultValue);
        }
        #endregion


        #region "查询指定品牌的品牌名称信息； by 赵国伟  2012-04-02"
        /// <summary>
        /// 查询指定品牌的品牌名称信息；
        /// </summary>
        /// <param name="brandIds"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryBrands(List<string> brandIds)
        {
            string cond = "";
            if (brandIds != null)
                cond += "BrandID in('" + string.Join("','", brandIds.ToArray()) + "') ";
            string orderBy = GetOrderBy(0);
            return dalBrand.Find(cond, orderBy);
        }
        #endregion

        #region "查询指定的状态的品牌信息并输出为下拉选项； by 赵国伟 2012-04-02"
        /// <summary>
        /// 查询指定的状态的品牌信息并输出为下拉选项；
        /// </summary>
        /// <param name="IsAvailable"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryBrandsToSelectOption(bool IsAvailable, string defaultValue)
        {
            BrandsManage bllBrand = new BrandsManage();
            List<IDNameObj> listOfData = bllBrand.QueryBrands(IsAvailable, 0);
            return BLLHelper.GenerateToSelectOption(listOfData, defaultValue);
        }
        #endregion

        #region "查询指定品牌的信息并存为Hash结构； by 赵国伟 2012-04-02"
        /// <summary>
        /// 查询指定品牌的信息并存为Hash结构；
        /// </summary>
        /// <param name="brandIds"></param>
        /// <returns></returns>
        public Dictionary<int, string> QueryBrandsToHash(List<string> brandIds)
        {
            List<IDNameObj> listOfData = QueryBrands(brandIds);
            return BLLHelper.GenerateToHash(listOfData);
        }
        #endregion

        #region "查询指定分类下的品牌信息； by 赵国伟 2012-05-13"
        /// <summary>
        /// 查询指定分类下的品牌信息；
        /// </summary>
        /// <param name="fCategoryID"></param>
        /// <param name="sCategoryID"></param>
        /// <param name="tCategoryID"></param>
        /// <param name="isAvailable"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryBrands(int fCategoryID, int sCategoryID, int tCategoryID, bool isAvailable)
        {
            string cond = " ";
            if (isAvailable)
                cond = " Status='" + Status.正用.ToString() + "' ";
            else
                cond = " status<>'" + Status.已删除.ToString() + "'";
            if (tCategoryID > 0)
            {
                cond += "and BrandID in(Select BrandID From R_Categories_Brands Where  CategoryID=" + fCategoryID + ")";
            }
            else
            {
                if (sCategoryID > 0)
                {
                    cond += "and BrandID in(Select BrandID From R_Categories_Brands Where CategoryID in(Select CategoryID From Categories Where status='正用' and (categoryID=" + sCategoryID + " or ParentID=" + sCategoryID + ")) )";
                }
                else
                {
                    if (fCategoryID > 0)
                    {
                        cond += "and BrandID in(Select BrandID From R_Categories_Brands Where ";
                        cond += " CategoryID=" + fCategoryID;
                        cond += " or ";
                        cond += "CategoryID in(Select CategoryID From Categories Where status='正用' and ParentID=" + fCategoryID + ") ";
                        cond += " or ";
                        cond += "CategoryID in(Select CategoryID from Categories Where status='正用' and ParentID in (Select CategoryID From Categories Where status='正用' and ParentID=" + fCategoryID + ") )";
                        cond +=" )";
                    }
                }
            }
            return dalBrand.Find(cond, "OrderValue,BrandID desc");
        }
        #endregion

        #region "查询相关商品的品牌； by 赵国伟 2012-05-22"
        /// <summary>
        /// 查询相关商品的品牌；
        /// </summary>
        /// <param name="listOfGoodsIDs"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryRelateBrand(List<string> listOfGoodsIDs)
        {
            string cond = "BrandID in(Select BrandID From Goods Where Status='" + Status.正用.ToString() + "' and SaleStatus='" + GoodsSaleStatus.已上架.ToString() + "')";
            cond += " and Status='" + Status.正用.ToString() + "' ";
            return dalBrand.Find(cond, "OrderValue,BrandID desc");
        }
        #endregion

        


    }
}
